#@laybase() 
#define main()

<style>

.col-md-12 {
	position: relative;
	min-height: 100px;
}

.control {
	width: 80%;
    position: absolute;
    top: 10px;
    left: 15px;
}

.control select {
	display: inline-block;
    width: 16%;
}

.control input {
	display: inline-block;
    width: 12%;
}

.control button {
	margin-top: -5px;
}

#fuiouSignModal .modal-body .getsign {
	width: 132px;
    margin: 0 auto;
    display: block;
}

#fuiouSignModal .modal-body .signmsg .radiobox {
	margin: 15px 0;
}

#fuiouSignModal .modal-body .signmsg .errbox {
	display: none;
}

#fuiouSignModal .modal-body .signmsg .radiobox a {
	display: inline-block;
	margin-right: 15px;
    width: 90px;
    text-align: center;
    height: 36px;
    line-height: 34px;
    border: 1px solid #ddd;
    color: #666;
    text-decoration: none;
}

#fuiouSignModal .modal-body .signmsg .radiobox a i {
	padding-right: 3px;
}

#fuiouSignModal .modal-body .signmsg .radiobox a i.success {
	font-size: 15px;
	color: #0c3;
}

#fuiouSignModal .modal-body .signmsg .radiobox a i.error {
	font-size: 12px;
	color: #d9534f;
}

#fuiouSignModal .modal-body .signmsg .radiobox a.active {
	border-color: #337ab7;
	color: #337ab7;
}

#fuiouSignModal .modal-body .signmsg .radiobox a:hover {
	text-decoration: none;
}

#fuiouSignModal .modal-body .signmsg .errbox label {
	font-weight: normal;
	vertical-align: top;
}

#fuiouSignModal .modal-body .signmsg .errbox label {
	font-weight: normal;
	vertical-align: top;
}

#fuiouSignModal .modal-body .signmsg .errbox .form-control {
	display: inline-block;
	width: 56%;
}

.datatable a {
	white-space: nowrap;
}

#alert_confirm .modal-body {
	padding: 0;
	font-size: 14px;
	text-align: left;
	padding: 5px;
	overflow: auto;
}

#alert_confirm .modal-body div {
	line-height: 32px;
}

</style>
<div class="modal fade" id="fuiouSignModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">提示</h4>
      </div>
      <div class="modal-body">
      	         
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary confirm" data-loading-text="请稍后...">提交</button>
      </div>
    </div>
  </div>
</div>

<div class="col-md-12" id="app">
	<div class="control">
		#@searchTab("opt",["苏宁","绿城"],["苏宁","绿城"])
		#@searchTab("state",["待签约","签约失败","签约成功"],[-1,0,1])
	</div>
	<table class="datatable" id="datatable" data-unique-id="id"></table>
</div>

<script src="/assets/js/bootstrap-table.min.js"></script>
<script src="/assets/js/bootstrap-table-zh-CN.js"></script>
<script>
	$(function(){
		var $table = $('#datatable'),
			searchDom = $("#searchbtn"),
			modalDom = $("#fuiouSignModal"),
			modalBody = modalDom.find(".modal-body"),
			modalTitle = modalDom.find(".modal-title"),
			modalConfirm = modalDom.find(".confirm"),
			signForm = $("#fuiouSignForm"),
			$allData = null
			
		axios.get("/fuioudq/getSignList")
		.then(function(res){
			console.log(res.data)
			var data = res.data,
				$data = data.data,
				titles = []
			
			titles.push({
				field: "operation",
				title: "操作"
			})
			
			for(var i = 0;i < $data.length;i++){
				$data[i].id = i
				var btnHtml = "<a href='/fuioudq/goSignControl/"+ $data[i].order_id +"' target='_blank' class='show-modal-hook'>签约</a>&nbsp;<a href='javascript:;' data-id='"+ i +"' data-orderid='"+ $data[i].order_id +"' class='setsign-hook'>录入</a>"
				if ($data[i].opt === '绿城') {
					btnHtml = "<a href='/llpay/goSignControl/"+ $data[i].order_id +"' class='llsign-hook' target='_blank'>签约</a>&nbsp;<a href='javascript:;' data-id='"+ i +"' data-orderid='"+ $data[i].order_id +"' class='queryresult-hook'>查询</a>"
				}
				$data[i].operation = btnHtml
				if(_x.isEmpty($data[i].dqContractSt)){
					$data[i].dqContractSt = "待签约"
				}else if($data[i].dqContractSt === 0){
					$data[i].dqContractSt = "<span style='color: #d9534f;'>签约失败</span>"
				}else if($data[i].dqContractSt === 1){
					$data[i].dqContractSt = "<span style='color: #0c3;'>签约成功</span>"
				}
				
				if(_x.isEmpty($data[i].dqInputSt)){
					$data[i].dqInputSt = "待录入"
				}else if($data[i].dqInputSt === 0){
					$data[i].dqInputSt = "<span style='color: #d9534f;'>录入失败</span>"
				}else if($data[i].dqInputSt === 1){
					$data[i].dqInputSt = "<span style='color: #0c3;'>录入成功</span>"
				}
			}
			
			for(var i = 0;i < data.names.length;i++){
				titles.push({
					field: data.fields[i],
					title: data.names[i]
				})
			}
			
			$allData = $data
			
			createTable(titles,$data)
		})
		
		//创建表格
		function createTable(titles,data){
			$table.bootstrapTable({
				uniqueId: 'id',	
				search: true,
				pagination: true, 
	            pageSize: 20,
			    columns: titles,
			    data: data,
			    sortStable:true,
			    onClickRow: function (row, tr,field) {
					console.log(row)
				    if(field === "operation"){
				    	editModal(row.order_id)
				    }
				},
			});		
		}
		
		$table.on("click","a.show-modal-hook",function(){
			modalDom.modal("show")
		})
		
		function editModal(orderId){
			modalConfirm.data("orderid",orderId)
			var html = "<div class='signmsg'>"+
			      	   "	<div class='radiobox'>"+
		      		   "		<a href='javascript:;' class='radio-hook' data-code='1'><i class='iconfont success'>&#xe666;</i>签约成功</a>"+
		      		   "		<a href='javascript:;' class='radio-hook' data-code='0'><i class='iconfont error'>&#xe701;</i>签约失败</a>"+
		      		   "	</div>"+
		      		   "	<div class='errbox'>"+
		      		   "		<label for='signerrmsg'>失败原因：</label>"+
			      	   "		<textarea class='errmsg form-control' id='signerrmsg'></textarea>"+
		      		   "	</div>"+
			      	   "</div>";
			modalBody.html(html)
		}
		
		modalBody.on("click","a.radio-hook",function(){
			var that = $(this),
				code = that.data("code")
			that.addClass("active").siblings("a.radio-hook").removeClass("active")
			if(code == 0){
				modalBody.find(".errbox").show()
			}else{
				modalBody.find(".errbox").hide()
			}
		})
		
		modalConfirm.click(function(){
			var that = $(this),
				mark = true
			if(modalBody.find("a.active").length === 0){
				mark = false
				_x.show_alert_sh("请选择标签！",false)
			}
			
			if(modalBody.find("a.active").length === 1 && modalBody.find("a.active").data("code") == 0 && _x.isEmpty($("#signerrmsg").val())){
				$("#signerrmsg").focus().parent().addClass("has-error")
				_x.show_alert_sh("请填写失败原因！",false)
				mark = false
			}
			
			if(mark){
				var reson = $("#signerrmsg").val()
				if(modalBody.find("a.active").data("code") == 1){
					reson = ""
				}
				var json = {
					state: modalBody.find("a.active").data("code"),
					order_id: that.data("orderid"),
					reson: reson
				}
				that.button('loading')
				axios.post("/fuioudq/submitSignResult",_x.qsStringify(json))
				.then(function(res){
					console.log(res.data)
					if(res.data.code == 1){
						modalDom.modal("hide")
						_x.show_alert_sh(res.data.msg,true)
					}else{
						_x.show_alert_sh(res.data.msg,false)
					}
					that.button('reset')
				})
				.catch(function(){
					_x.show_alert_sh("提交失败！",false)
					that.button('reset')
				})
			}
		})
		
		//搜索
		var searchtabDom = $(".searchtab-wrapper")
		searchtabDom.children("span").on("click",function(){
			var data = {}
			searchtabDom.each(function(){
				var input = $(this).children("input"),
					key = input.attr("key"),
					val = input.val()
				data[key] = val
			})

			axios.post("/fuioudq/getSignList",_x.qsStringify(data))
			.then(function(res){
				console.log(res.data)
				var nowData = res.data.data

				for(var i = 0;i < nowData.length;i++){
					nowData[i].id = i
					var btnHtml = "<a href='/fuioudq/goSignControl/"+ nowData[i].order_id +"' target='_blank' class='show-modal-hook'>签约</a>&nbsp;<a href='javascript:;' data-id='"+ i +"' data-orderid='"+ nowData[i].order_id +"' class='setsign-hook'>录入</a>"
					if (nowData[i].opt === '绿城') {
						btnHtml = "<a href='/llpay/goSignControl/"+ nowData[i].order_id +"' class='llsign-hook' target='_blank'>签约</a>&nbsp;<a href='javascript:;' data-id='"+ i +"' data-orderid='"+ nowData[i].order_id +"' class='queryresult-hook'>查询</a>"
					}
					nowData[i].operation = btnHtml
					if(_x.isEmpty(nowData[i].dqContractSt)){
						nowData[i].dqContractSt = "待签约"
					}else if(nowData[i].dqContractSt === 0){
						nowData[i].dqContractSt = "<span style='color: #d9534f;'>签约失败</span>"
					}else if(nowData[i].dqContractSt === 1){
						nowData[i].dqContractSt = "<span style='color: #0c3;'>签约成功</span>"
					}
					
					if(_x.isEmpty(nowData[i].dqInputSt)){
						nowData[i].dqInputSt = "待录入"
					}else if(nowData[i].dqInputSt === 0){
						nowData[i].dqInputSt = "<span style='color: #d9534f;'>录入失败</span>"
					}else if(nowData[i].dqInputSt === 1){
						nowData[i].dqInputSt = "<span style='color: #0c3;'>录入成功</span>"
					}
				}
				$table.bootstrapTable("load",nowData)
			})
		})	
		
		
		$table.on("click","a.setsign-hook",function(){
			var that = $(this),
				uniqueId = that.data("id"),
				option = {
					isShowBg: true,
					text: randerMsg(uniqueId),
					fn: function(modalDom,btn,callback){
						axios.get("/fuioudq/projectApply",{
							params: {
								orderId: that.data("orderid")
							}
						})
						.then(function(res){
							console.log(res.data)
							if(res.data.code == "0000"){
								_x.show_alert_sh(res.data.msg,true)
							}else{
								_x.show_alert_sh(res.data.msg,false)
							}
							callback()
						})
						.catch(function(){
							_x.show_alert_sh("录入失败，请稍后重试！",false)
							callback()
						})
					}
				}
			_x.show_alert_confirm(option)
		})
		
		function randerMsg(uniqueId){
			var rowData = $table.bootstrapTable('getRowByUniqueId',uniqueId),
				html = "<div><span>订单号：</span><span>"+ rowData.order_id +"</span></div>"+
					   "<div><span>地区：</span><span>"+ rowData.area +"</span></div>"+
					   "<div><span>客户姓名：</span><span>"+ rowData.name +"</span></div>"+
					   "<div><span>客户电话：</span><span>"+ rowData.phone +"</span></div>"+
					   "<div><span>期数：</span><span>"+ rowData.term +"</span></div>"+
					   "<div><span>贷款金额：</span><span>"+ rowData.amt +"</span></div>"+
					   "<div><span>月还款金额：</span><span>"+ rowData.monthlyRepayAmt +"</span></div>"+
					   "<div><span>签约日期：</span><span>"+ rowData.signDate +"</span></div>"+
					   "<div><span>录入日期：</span><span>"+ rowData.lendingDate +"</span></div>";
			return html
		}
		
		$table.on("click","a.queryresult-hook",function(){
			var that = $(this),
				order_id = that.data("orderid"),
				option = {
					isShowBg: true,
					text: "",
					fn: function(modalDom,btn,callback){
						callback()
					}
				}
			llSignSearch(order_id,function(data){
				var html = ""
				if (data.ret_code == '0000') {
					html += "<div><span>签约成功</span></div>"+
							"<div><span>结果说明：</span><span>"+ data.ret_msg +"</span></div>"
					for (var i = 0; i< data.agreement_list.length; i++) {
						var json = data.agreement_list[i]
						for (var k in json) {
							if (k === 'bank_code') {
								html += "<div><span>银行代号：</span><span>"+ json[k] +"</span></div>"
							} else if (k === 'bank_name') {
								html += "<div><span>银行名称：</span><span>"+ json[k] +"</span></div>"
							} else if (k === 'card_no') {
								html += "<div><span>卡尾号：</span><span>"+ json[k] +"</span></div>"
							} else if (k === 'card_type') {
								html += "<div><span>卡类别：</span><span>"+ json[k] +"</span></div>"
							} else if (k === 'no_agree') {
								html += "<div><span>协议号：</span><span>"+ json[k] +"</span></div>"
							}
						}
					}
				} else {
					html += "<div><span>签约失败</span></div>"+
							"<div><span>结果说明：</span><span>"+ data.ret_msg +"</span></div>"
				}
				option.text = html
				_x.show_alert_confirm(option)
			})
			
		})
		
		function llSignSearch(order_id, callback){
			
			axios.post("/llpay/signedSearch",_x.qsStringify({order_id: order_id}))
			.then(function(res){
				console.log(res.data)
				callback(res.data)
			})
			.catch(function(){
				_x.show_alert_sh("查询异常，请稍后重试！",false)
			})
		}
	})
</script>
#end